package com.frxuexi.lib_base.http

import com.blankj.utilcode.util.SPUtils
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

object RetrofitUtils {

    val client by lazy {
        OkHttpClient.Builder()
            .readTimeout(10,TimeUnit.SECONDS)
            .writeTimeout(10,TimeUnit.SECONDS)
            .connectTimeout(10,TimeUnit.SECONDS)
            .addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
            .addInterceptor {
                val request = it.request().newBuilder()
                    .addHeader("Authori-zation", SPUtils.getInstance().getString(Const.TOKEN, ""))
                    .build()
                it.proceed(request)
            }
            .build()
    }

    val retrofit by lazy {
        Retrofit.Builder()
            .baseUrl(Const.BASE_URL)
            .client(client)
            .addConverterFactory(GsonConverterFactory.create())
            .build()
    }

    val clientVideo by lazy {
        OkHttpClient.Builder()
            .readTimeout(60,TimeUnit.SECONDS)
            .writeTimeout(60,TimeUnit.SECONDS)
            .connectTimeout(60,TimeUnit.SECONDS)
            .addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
            .addInterceptor {
                val request = it.request().newBuilder()
//                    .addHeader("token", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJsciIsImV4cCI6MTc2MTAxNjE4Mn0.wnfRyZg1LRhSL42UmixE_2yBPqu92vzsDhJAhSSp-vU")
                    .addHeader("token", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJhdXRoMCIsImV4cCI6MTc2MjkzMzA2OCwidXNlcm5hbWUiOiJsciJ9.OmI6V6O221lWPckC_h6XCnszjUHlDJ8DOvY_Kdq_Muc")
                    .build()
                it.proceed(request)
            }
            .build()
    }

    val retrofitVideo by lazy {
        Retrofit.Builder()
            .baseUrl(Const.BASE_URL_MYVIDEO)
            .client(clientVideo)
            .addConverterFactory(GsonConverterFactory.create())
            .build()
    }
}